package HashMap;

import javax.net.ssl.SSLEngineResult.Status;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowListener;
import java.util.Iterator;

public class ModifyPanel extends JPanel 
{
	private JTextField textField;
	private JButton btnSelect;
	private JLabel lblResult;
	private JLabel lbl_Res_Msg;
	private JButton btnDelete;
	private JButton btnUpdate;
	private JOptionPane jop = new JOptionPane();
	private JPanel panel;
	//private UpdateFrame panel_1;
	private JLabel lblId;
	private JLabel lbl_Id;
	private JLabel lblName;
	private JTextField tF_Name;
	private JLabel lblBirthday;
	private JTextField tF_Birthday;
	private JLabel lblAddress;
	private JTextField tF_Address;
	private JLabel Message;
	

	/**
	 * Create the panel.
	 */
	public ModifyPanel() 
	{
		setLayout(null);
		add(getTextField());
		add(getBtnSelect());
		add(getLblResult());
		add(getLbl_Res_Msg());
		add(getBtnDelete());
		add(getBtnUpdate());
		add(getLblId());
		add(getLbl_Id());
		add(getLblName());
		add(getTF_Name());
		add(getLblBirthday());
		add(getTF_Birthday());
		add(getLblAddress());
		add(getTF_Address());
		add(getMessage());
		


	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(12, 10, 303, 21);
			textField.setColumns(10);
		}
		return textField;
	}
	private JButton getBtnSelect() 
	{
		if (btnSelect == null) 
		{
			btnSelect = new JButton("Select");
			btnSelect.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e) 
				{
					String sel_Con = textField.getText();

					Iterator<String> key = MapFrame.map.keySet().iterator();
					Iterator<Data> values = MapFrame.map.values().iterator();
					
					while (key.hasNext()) 
					{
						String id = key.next();
						System.out.println(id);
						Data d = values.next();
						
						if (id.equals(sel_Con)) 
						{
							lbl_Res_Msg.setText(d.getData()+"\n");
							lbl_Id.setText(sel_Con);
							tF_Name.setText(d.getName());
							tF_Birthday.setText(d.getBirthday());
							tF_Address.setText(d.getAddress());
						}
						else if (d.getName().contains(sel_Con)) 
						{
							lbl_Res_Msg.setText(d.getData()+"\n");
							lbl_Id.setText(sel_Con);
							tF_Name.setText(d.getName());
							tF_Birthday.setText(d.getBirthday());
							tF_Address.setText(d.getAddress());
						}
						else if (d.getBirthday().contains(sel_Con)) 
						{
							lbl_Res_Msg.setText(d.getData()+"\n");
							lbl_Id.setText(sel_Con);
							tF_Name.setText(d.getName());
							tF_Birthday.setText(d.getBirthday());
							tF_Address.setText(d.getAddress());
						}
						else if (d.getAddress().contains(sel_Con)) 
						{
							lbl_Res_Msg.setText(d.getData()+"\n");
							lbl_Id.setText(sel_Con);
							tF_Name.setText(d.getName());
							tF_Birthday.setText(d.getBirthday());
							tF_Address.setText(d.getAddress());
						}
						
						
						/*else 
						{
							textArea.append("Nothing EXIST");
						}*/
					}
					
					//trim() = 공백을 모두 지워라
					/*if (textArea.getText().trim().equals("")) 
					{
						textArea.append("Nothing EXIST");
					}*/
					if (lbl_Res_Msg.getText().equals("")) 
					{
						lbl_Res_Msg.setText("Nothing EXIST");
						textField.setText("");
					}
				
				}
			});
			btnSelect.setBounds(327, 9, 97, 23);
		}
		return btnSelect;
	}
	private JLabel getLblResult() {
		if (lblResult == null) {
			lblResult = new JLabel("Result : ");
			lblResult.setBounds(12, 41, 57, 15);
		}
		return lblResult;
	}
	private JLabel getLbl_Res_Msg() {
		if (lbl_Res_Msg == null) {
			lbl_Res_Msg = new JLabel(" ");
			lbl_Res_Msg.setBounds(12, 66, 412, 23);
		}
		return lbl_Res_Msg;
	}
	private JButton getBtnDelete() 
	{
		if (btnDelete == null) 
		{
			btnDelete = new JButton("Delete");
			btnDelete.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e) 
				{
					String sel_Con=textField.getText();
					
					if (MapFrame.map.get(sel_Con)!=null) 
					{
						MapFrame.map.remove(sel_Con);
						Message.setText("Delete Success");
					}
				}
			});
			btnDelete.setBounds(12, 230, 192, 31);
		}
		return btnDelete;
	}
	private JButton getBtnUpdate() 
	{
		if (btnUpdate == null) 
		{
			btnUpdate = new JButton("Update");
			btnUpdate.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e) 
				{
					String sel_Con = textField.getText();
					
					String newName = tF_Name.getText();
					String newBirth = tF_Birthday.getText();
					String newAddress = tF_Address.getText();
					
					Iterator<String> key = MapFrame.map.keySet().iterator();
					Iterator<Data> values = MapFrame.map.values().iterator();
					
					while (key.hasNext()) 
					{
						String id = key.next();
						System.out.println(id);
						Data d = values.next();
						
						if (id.equals(sel_Con)) 
						{
							d.setName(newName);
							d.setBirthday(newBirth);
							d.setAddress(newAddress);
							
							Message.setText("Update Success");
							break;
						}
					}
				}
			});
			btnUpdate.setBounds(216, 230, 208, 31);
		}
		return btnUpdate;
	}
	private JLabel getLblId() 
	{
		if (lblId == null) 
		{
			lblId = new JLabel("ID : ");
			lblId.setBounds(12, 99, 57, 15);
		}
		return lblId;
	}
	private JLabel getLbl_Id() 
	{
		if (lbl_Id == null)
		{
			lbl_Id = new JLabel("");
			lbl_Id.setBounds(91, 99, 333, 15);
		}
		return lbl_Id;
	}
	private JLabel getLblName() 
	{
		if (lblName == null) 
		{
			lblName = new JLabel("Name : ");
			lblName.setBounds(12, 127, 57, 15);
		}
		return lblName;
	}
	private JTextField getTF_Name() 
	{
		if (tF_Name == null) 
		{
			tF_Name = new JTextField();
			tF_Name.setBounds(91, 124, 333, 21);
			tF_Name.setColumns(10);
		}
		return tF_Name;
	}
	private JLabel getLblBirthday() 
	{
		if (lblBirthday == null) 
		{
			lblBirthday = new JLabel("Birthday");
			lblBirthday.setBounds(12, 158, 57, 15);
		}
		return lblBirthday;
	}
	private JTextField getTF_Birthday() 
	{
		if (tF_Birthday == null) 
		{
			tF_Birthday = new JTextField();
			tF_Birthday.setBounds(91, 155, 333, 21);
			tF_Birthday.setColumns(10);
		}
		return tF_Birthday;
	}
	private JLabel getLblAddress() 
	{
		if (lblAddress == null) 
		{
			lblAddress = new JLabel("Address : ");
			lblAddress.setBounds(12, 192, 67, 15);
		}
		return lblAddress;
	}
	private JTextField getTF_Address() 
	{
		if (tF_Address == null) 
		{
			tF_Address = new JTextField();
			tF_Address.setBounds(91, 189, 333, 21);
			tF_Address.setColumns(10);
		}
		return tF_Address;
	}
	private JLabel getMessage()
	{
		if (Message == null) 
		{
			Message = new JLabel("");
			Message.setBounds(12, 275, 412, 15);
		}
		return Message;
	}
}
